<?php

namespace App\Admin\Controllers;

use App\Models\Business;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class BusinessController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('商家');
            $content->description('');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('商家管理');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新增');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Business::class, function (Grid $grid) {

            $grid->id('ID')->sortable();

            $grid->name('厂商名称');

            $grid->licence_img('营业执照')->fancyImage('',100,100);
            $grid->phone('联系方式');
            //状态列
            $states = [
                'on' => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                'off' => ['value' => 2, 'text' => '禁用', 'color' => 'default'],
            ];
            $grid->status('状态')->switch($states);

            //关掉批量删除
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });

            $grid->filter(function ($filter) {
                // 禁用id查询框
                $filter->disableIdFilter();
                //分类查询
                $filter->equal('status', '状态')->select(['1'=>'正常','2'=>'禁用']);
            });

            $grid->created_at('创建时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Business::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name', '标题')->rules('required');
            $form->text('phone', '电话')->rules('required');
            $states = [
                'on' => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                'off' => ['value' => 2, 'text' => '禁用', 'color' => 'default'],
            ];
            $form->switch('status', '状态')->states($states);
        });
    }
}
